<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Index_Test extends Controller_Index_Base
{
    public function before()
    {
        die('dsds');
    }
    
    public function action_test1()
    {
        $product = ORM::factory('product', 157869);
        Model_ProductParser_FromShop_Wildberries::instance()->parse($product);  
    }
    
    public function action_test2()
    {
        set_time_limit(0);
        
        /*
        515     Quelle          цвет
        1001    Lamoda          цвет, материал, сезон
        1186    Proskater       цвет
        1326    TOPBRANDS       цвет, материал
        1477    ShopTime        -
        1757    BUTIK           цвет
        2775    MEXX            -
        2777    TomTailor       -
        3761    SHOWROOMS       материал
        4102    Wildberries     цвет, материал (состав), сезон, стиль
        */
        
        $params = array();
        $desc   = array();
        
        $products = ORM::factory('product')
                        ->where('shop_id', '=', 4102)
                        ->find_all();
        foreach ($products as $product) {
            if ($product->params) {
                foreach (json_decode($product->params, true) as $name => $value) {
                    $value = trim($value);
                    $name  = mb_strtolower($name);
                    if (!isset($params[$name])) {
                        $params[$name] = array();       
                    }
                    $values = explode(',', $value);
                    foreach ($values as $value) {
                        $value = trim($value);
                        if (!isset($params[$name][$value])) {
                            $params[$name][$value] = 1;    
                        }
                        $params[$name][$value]++; 
                    }                    
                }
            }
            if ($product->description) {
                $description = explode(' ', $product->description);
                foreach ($description as $item) {
                    $item = trim($item);
                    if (!isset($desc[$item])) {
                        $desc[$item] = 1;    
                    }
                    $desc[$item]++;       
                }
            }         
        }
        foreach ($params as &$group) {
            arsort($group);    
        }
        arsort($desc);
        
        echo '<pre>';
        print_r($params);
        //print_r($desc);
    } 
    
    public function action_test3()
    {
        die();
        set_time_limit(0);
        
        function get_parents($category, &$cats) {
            $text = array();
            $text[] = mb_strtolower($category->name);
            $cat = $cats[$category->id];
            while ($cat->parent_id != 0) {
                $cat = $cats[$cat->parent_id];
                $text[] = mb_strtolower($cat->name);
            }
            return implode(' - ', $text);    
        }  
        
        $categories = ORM::factory('category')->find_all();
        $categories = Model_Main::process_array($categories); 
        
        foreach ($categories as $category) {
            $text = get_parents($category, $categories); 
            
            $category->nominative_form = $text; 
            $category->accusative_form = $text;
            $category->save();  
        }
    }
} 
